package api;

import cn.hutool.json.JSONUtil;
import comment.ApplicationVariable;
import model.ResultData;
import model.UserInfo;
import util.SessionUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/session")
public class GetSessionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType(ApplicationVariable.APPLICATION_JSON);
        String data = "";
        int state = -1;
        String msg = "未知错误";
        UserInfo userInfo = SessionUtil.getSessionUserInfo(req);
        if(userInfo != null){
            //已经登录
            state = 200;
            data = userInfo.getUsername();
            msg = "已登录";
        }
        else {
            //未登录
            msg = "未登录";
        }
        resp.getWriter().println(JSONUtil.toJsonStr(new ResultData(state,data,msg)));
    }
}
